﻿using D.B.Data.API.Applications.Command.LeaveCommand;
using D.B.Data.Domain;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;

namespace D.B.Data.API.Applications.CommandHandler.LeaveCommandHandler
{
    public class QueryDeptBoosCommandHandler : IRequestHandler<QueryDeptBoosCommand, APIResult<string>>
    {
        private readonly IBaseRepository<DeptModel> baseRepository;
        private readonly IBaseRepository<UserModel> userRepository;

        public QueryDeptBoosCommandHandler(IBaseRepository<DeptModel> baseRepository, IBaseRepository<UserModel> userRepository)
        {
            this.baseRepository = baseRepository;
            this.userRepository = userRepository;
        }

        public Task<APIResult<string>> Handle(QueryDeptBoosCommand request, CancellationToken cancellationToken)
        {
            var res = baseRepository.GetAll().FirstOrDefault(s => s.Id.Equals(request.Id)).BoosId;
            var UserName = userRepository.GetAll().FirstOrDefault(s => s.Id == res).UserName;


            APIResult<string> aPIResult = new APIResult<string>();

            if (UserName == null)
            {
                aPIResult.Code = EnumCode.File;
                aPIResult.Msg = "获取失败";

            }
            else
            {
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Msg = "获取成功";
                aPIResult.Data= UserName;
            }
            return Task.FromResult(aPIResult);
        }
    }
}
